<template>
	<view>
		<view class="feedback-title">意见反馈：</view>
		<view class="feedback-conent">
			<textarea class="feedback-textarea" v-model="content" placeholder="请输入你要反馈的问题" />
			</view>
		<view class="feedback-title">反馈图片：</view>
		<view class="feedback-image-box">
			<view class="feedback-image-item" v-for="item,index in imageList" :key="index">
				<view class="close-icon" @click="delImage(index)">
					<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
				</view>
				<view class="image-box">
					<image :src="item.url" mode="aspectFill"></image>
				</view>
			</view>
			<!-- 添加图片 -->
			<view class="feedback-image-item" @click="addImage" v-if="imageList.length < 5">
				<view class="image-box">
					<uni-icons type="plusempty" size="50" color="#eee"></uni-icons>
				</view>
			</view>
		</view>
		<button class="feedback-button" type="primary" @click="submit">提交反馈意见</button>
	</view>
</template>

<script>
	import {lorem, getType} from '@/utils/tools.js'
	import {update_feedback} from '@/common/api/user.js'
	export default {
		data() {
			return {
				// 反馈图片数组
				imageList: [],
				// 反馈内容
				content: ''
			}
		},
		methods: {
			// 添加图片
			addImage() {
				uni.chooseImage({
					count: 5,
					success: (res) => {
						// 上传的图片数组
						const tempFilePaths = res.tempFilePaths
						// 遍历
						tempFilePaths.forEach((item,index) => {
							this.imageList.push({
								url: item
							})
						})
					}
				})
			},
			// 删除图片
			delImage(index) {
				this.imageList.splice(index, 1)
			},
			// 提交意见
			async submit() {
				uni.showLoading({
					title: '图片上传中'
				})
				let imagesPath = []
				
				// 上传图片的api每次只能只能上传一张,使用遍历上传
				for (let i = 0, len = this.imageList.length; i < len; i++) {
					// 图片blob路径
					let filePath = this.imageList[i].url
					// 文件随机名称
					let cloudPath = lorem()
					cloudPath += '.jpg'
					
					// 重新赋值，获上传后的图片路径
					filePath = await this._uploadFiles(filePath,cloudPath)
					
					imagesPath.push(filePath)
				}
				
				// 调用提交反馈函数
				this._updateFeedback({
					feedbackImages: imagesPath,
					content: this.content
				})	
				
			},
			// 上传文件函数 
			async _uploadFiles(filePath,cloudPath) {
				
				const result = await uniCloud.uploadFile({
					filePath,
					cloudPath
				})
				
				return result.fileID
			},
			// 上传意见反馈
			_updateFeedback({feedbackImages, content}) {
				update_feedback({
					feedbackImages,
					content
				})
				.then(res => {
					uni.showToast({
						title: '反馈提交成功',
						icon: 'none'
					})
					
					uni.hideLoading()
					
					setTimeout(() => {
						// 跳转到个人中心
						uni.switchTab({
							url: '/pages/tabbar/my/my'
						})
					},1000)
					
					// 提交完成后清空图片选择框
					this.imageList = []
					// 提交完成后清空文本
					this.content = ''
				})
				.catch(err => {
					uni.hideLoading()
					uni.showToast({
						title: '反馈提交失败,' + err,
						icon: 'none'
					})
				})
				
			}
		},
		watch: {
			// 监听图片的改变!保留5张图片
			imageList() {
				if(this.imageList.length > 5) {
					this.imageList.splice(5, this.imageList.length)
					console.log(this.imageList)
				}
			}
		}
	}
</script>

<style lang="scss">
	.feedback-title{
		font-size: 28upx;
		margin: 30upx;
		margin-bottom: 0;
		color: #666;
	}
	.feedback-conent{
		margin: 30upx;
		padding: 20upx;
		box-sizing: border-box;
		border: 1px solid #F5F5F5;
		.feedback-textarea{
			font-size: 24upx;
			width: 100%;
			height: 200upx;
		}
	}
	.feedback-image-box{
		display: flex;
		flex-wrap: wrap;
		padding: 20upx;
		.feedback-image-item{
			position: relative;
			width: 33.3333333%;
			height: 0;
			padding-bottom: 33.3333333%;
			box-sizing: border-box;
			.close-icon{
				display: flex;
				align-items: center;
				justify-content: center;
				position: absolute;
				right: 0;
				top: 0;
				width: 44upx;
				height: 44upx;
				border-radius: 50%;
				background-color: #ff5a5f;
				z-index: 2;
			}
			.image-box{
				display: flex;
				align-items: center;
				justify-content: center;
				position: absolute;
				top: 10upx;
				left: 10upx;
				right: 10upx;
				bottom: 10upx;
				border: 2upx solid #eee;
				overflow: hidden;
				border-radius: 10upx;
				image{
					width: 100%;
					height: 100%;
				}
			}
		}
	}
	.feedback-button{
		margin: 0 30upx;
		background-color: $mk-base-color;
		font-size: 32upx;
	}
</style>
